package com.example.demo.service.impl;

import com.example.demo.entity.Safetyinspectionswitchsensor;
import com.example.demo.handle.SwitchSensorListParseHandler;
import com.example.demo.mapper.SafetyinspectionswitchsensorMapper;
import com.example.demo.service.SafetyinspectionswitchsensorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-16
 */
@Service
public class SafetyinspectionswitchsensorServiceImpl extends ServiceImpl<SafetyinspectionswitchsensorMapper, Safetyinspectionswitchsensor> implements SafetyinspectionswitchsensorService {

    @Autowired
    private SafetyinspectionswitchsensorMapper safetyinspectionswitchsensorMapper;

    @Override
    public Integer insertSwitchSensorData(String switchSensorXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            SwitchSensorListParseHandler switchSensorListParseHandler = new SwitchSensorListParseHandler();
            try {
                //读取井口信息文件
                parser.parse(switchSensorXml, switchSensorListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + switchSensorListParseHandler.getSafetyinspectionswitchsensorArrayList().size() + "个信息");
            // 根据minecode去查找数据库
            for (Safetyinspectionswitchsensor safetyinspectionswitchsensor : switchSensorListParseHandler.getSafetyinspectionswitchsensorArrayList()) {
                System.out.println(safetyinspectionswitchsensor.getCsMineCode());
                System.out.println(safetyinspectionswitchsensor.getCsDataTime());
                System.out.println(safetyinspectionswitchsensor.getSsStationCode());
                System.out.println(safetyinspectionswitchsensor.getSsTransducerCode());
                System.out.println(safetyinspectionswitchsensor.getSsTransducerType());
                System.out.println(safetyinspectionswitchsensor.getSsTransducerName());
                System.out.println(safetyinspectionswitchsensor.getSsTransducerPoint());
                System.out.println(safetyinspectionswitchsensor.getSsGisnodeCode());
                System.out.println(safetyinspectionswitchsensor.getSsPoffareaCode());

                System.out.println("----------------------------安全监控信息finish---------------------------------");

                safetyinspectionswitchsensorMapper.insertSwitchSensorData(safetyinspectionswitchsensor.getCsMineCode(),safetyinspectionswitchsensor.getCsDataTime(),safetyinspectionswitchsensor.getSsStationCode(),safetyinspectionswitchsensor.getSsTransducerCode(),safetyinspectionswitchsensor.getSsTransducerType(),safetyinspectionswitchsensor.getSsTransducerName(),safetyinspectionswitchsensor.getSsTransducerPoint(),safetyinspectionswitchsensor.getSsGisnodeCode(),safetyinspectionswitchsensor.getSsPoffareaCode());
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
